package com.ruoyi.find.service.impl;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.ruoyi.common.core.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.find.mapper.FurnitureWorkOrderMapper;
import com.ruoyi.find.mapper.FurnitureMapper;
import com.ruoyi.find.domain.FurnitureWorkOrder;
import com.ruoyi.find.domain.Furniture;
import com.ruoyi.find.service.IFurnitureWorkOrderService;

/**
 * 家具工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-05-24
 */
@Service
public class FurnitureWorkOrderServiceImpl implements IFurnitureWorkOrderService 
{
    @Autowired
    private FurnitureWorkOrderMapper furnitureWorkOrderMapper;

    @Autowired
    private FurnitureMapper furnitureMapper;

    /**
     * 查询家具工单
     * 
     * @param orderId 家具工单主键
     * @return 家具工单
     */
    @Override
    public FurnitureWorkOrder selectFurnitureWorkOrderByOrderId(Long orderId)
    {
        return furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
    }

    /**
     * 根据工单编号查询工单
     * 
     * @param orderCode 工单编号
     * @return 家具工单
     */
    @Override
    public FurnitureWorkOrder selectFurnitureWorkOrderByOrderCode(String orderCode)
    {
        return furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderCode(orderCode);
    }

    /**
     * 查询家具工单列表
     * 
     * @param furnitureWorkOrder 家具工单
     * @return 家具工单集合
     */
    @Override
    public List<FurnitureWorkOrder> selectFurnitureWorkOrderList(FurnitureWorkOrder furnitureWorkOrder)
    {
        return furnitureWorkOrderMapper.selectFurnitureWorkOrderList(furnitureWorkOrder);
    }

    /**
     * 新增家具工单
     * 
     * @param furnitureWorkOrder 家具工单
     * @return 结果
     */
    @Override
    public int insertFurnitureWorkOrder(FurnitureWorkOrder furnitureWorkOrder)
    {
        furnitureWorkOrder.setOrderTime(DateUtils.getNowDate());
        furnitureWorkOrder.setCreateTime(DateUtils.getNowDate());
        furnitureWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.insertFurnitureWorkOrder(furnitureWorkOrder);
    }

    /**
     * 修改家具工单
     * 
     * @param furnitureWorkOrder 家具工单
     * @return 结果
     */
    @Override
    public int updateFurnitureWorkOrder(FurnitureWorkOrder furnitureWorkOrder)
    {
        furnitureWorkOrder.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(furnitureWorkOrder);
    }

    /**
     * 批量删除家具工单
     * 
     * @param orderIds 需要删除的家具工单主键集合
     * @return 结果
     */
    @Override
    public int deleteFurnitureWorkOrderByOrderIds(Long[] orderIds)
    {
        return furnitureWorkOrderMapper.deleteFurnitureWorkOrderByOrderIds(orderIds);
    }

    /**
     * 删除家具工单信息
     * 
     * @param orderId 家具工单主键
     * @return 结果
     */
    @Override
    public int deleteFurnitureWorkOrderByOrderId(Long orderId)
    {
        return furnitureWorkOrderMapper.deleteFurnitureWorkOrderByOrderId(orderId);
    }

    /**
     * 查询买家的工单列表
     * 
     * @param buyerUserId 买家用户ID
     * @return 工单列表
     */
    @Override
    public List<FurnitureWorkOrder> selectWorkOrdersByBuyerId(Long buyerUserId)
    {
        return furnitureWorkOrderMapper.selectWorkOrdersByBuyerId(buyerUserId);
    }

    /**
     * 查询卖家的工单列表
     * 
     * @param sellerUserId 卖家用户ID
     * @return 工单列表
     */
    @Override
    public List<FurnitureWorkOrder> selectWorkOrdersBySellerId(Long sellerUserId)
    {
        return furnitureWorkOrderMapper.selectWorkOrdersBySellerId(sellerUserId);
    }

    /**
     * 根据状态查询工单
     * 
     * @param orderStatus 工单状态
     * @return 工单列表
     */
    @Override
    public List<FurnitureWorkOrder> selectWorkOrdersByStatus(Integer orderStatus)
    {
        return furnitureWorkOrderMapper.selectWorkOrdersByStatus(orderStatus);
    }

    /**
     * 生成工单编号
     * 
     * @return 工单编号
     */
    @Override
    public String generateOrderCode()
    {
        String dateStr = DateUtils.dateTimeNow("yyyyMMddHHmmss");
        String randomStr = UUID.randomUUID().toString().substring(0, 6).toUpperCase();
        return "FWO" + dateStr + randomStr;
    }

    /**
     * 确认工单
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int confirmOrder(Long orderId)
    {
        FurnitureWorkOrder order = furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
        if (order == null) {
            throw new RuntimeException("工单不存在");
        }
        if (order.getOrderStatus() != 1) {
            throw new RuntimeException("工单状态不正确，无法确认");
        }
        order.setOrderStatus(2);
        order.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(order);
    }

    /**
     * 支付工单
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int payOrder(Long orderId)
    {
        FurnitureWorkOrder order = furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
        if (order == null) {
            throw new RuntimeException("工单不存在");
        }
        if (order.getOrderStatus() != 2) {
            throw new RuntimeException("工单状态不正确，无法支付");
        }
        order.setOrderStatus(3);
        order.setPayTime(DateUtils.getNowDate());
        order.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(order);
    }

    /**
     * 发货
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int shipOrder(Long orderId)
    {
        FurnitureWorkOrder order = furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
        if (order == null) {
            throw new RuntimeException("工单不存在");
        }
        if (order.getOrderStatus() != 3) {
            throw new RuntimeException("工单状态不正确，无法发货");
        }
        order.setOrderStatus(4);
        order.setShipTime(DateUtils.getNowDate());
        order.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(order);
    }

    /**
     * 确认收货
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int receiveOrder(Long orderId)
    {
        FurnitureWorkOrder order = furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
        if (order == null) {
            throw new RuntimeException("工单不存在");
        }
        if (order.getOrderStatus() != 4) {
            throw new RuntimeException("工单状态不正确，无法确认收货");
        }
        order.setOrderStatus(5);
        order.setCompleteTime(DateUtils.getNowDate());
        order.setUpdateTime(DateUtils.getNowDate());
        
        // 如果是出售类型，将家具下架
        if (order.getTransactionType() == 1) {
            Furniture furniture = furnitureMapper.selectFurnitureByFurnitureId(order.getFurnitureId());
            if (furniture != null) {
                furniture.setStatus(0);
                furnitureMapper.updateFurniture(furniture);
            }
        }
        
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(order);
    }

    /**
     * 取消工单
     * 
     * @param orderId 工单ID
     * @return 结果
     */
    @Override
    public int cancelOrder(Long orderId)
    {
        FurnitureWorkOrder order = furnitureWorkOrderMapper.selectFurnitureWorkOrderByOrderId(orderId);
        if (order == null) {
            throw new RuntimeException("工单不存在");
        }
        // 只有待确认和待付款状态可以取消
        if (order.getOrderStatus() != 1 && order.getOrderStatus() != 2) {
            throw new RuntimeException("当前状态无法取消工单");
        }
        order.setOrderStatus(6);
        order.setUpdateTime(DateUtils.getNowDate());
        return furnitureWorkOrderMapper.updateFurnitureWorkOrder(order);
    }

    /**
     * 创建工单（包含业务逻辑校验）
     * 
     * @param order 工单信息
     * @return 结果
     */
    @Override
    public int createWorkOrder(FurnitureWorkOrder order)
    {
        // 检查家具是否存在
        Furniture furniture = furnitureMapper.selectFurnitureByFurnitureId(order.getFurnitureId());
        if (furniture == null) {
            throw new RuntimeException("家具不存在");
        }
        
        // 检查家具是否上架
        if (furniture.getStatus() != 1) {
            throw new RuntimeException("家具未上架或已下架");
        }
        
        // 检查是否是自己的家具
        if (order.getBuyerUserId().equals(furniture.getCreateBy())) {
            throw new RuntimeException("不能购买自己发布的家具");
        }
        
        // 设置工单信息
        order.setOrderCode(generateOrderCode());
        order.setFurnitureName(furniture.getFurnitureName());
        order.setTransactionType(furniture.getTransactionType());
        order.setTransactionAmount(furniture.getPrice());
        order.setOrderStatus(1); // 待确认
        order.setOrderTime(DateUtils.getNowDate());
        order.setCreateTime(DateUtils.getNowDate());
        order.setUpdateTime(DateUtils.getNowDate());
        
        return furnitureWorkOrderMapper.insertFurnitureWorkOrder(order);
    }
}